/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pass_client.admin;

import entities.Assignment;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import pasremote.PASRemote;

/**
 *
 * @author east429sara
 */
public class AssignmentListPanel extends javax.swing.JPanel {
    private PASRemote bean;
    private ArrayList<Long> asmtIDList;

    /**
     * Creates new form StudentListPanel
     */
    public AssignmentListPanel() {
        initComponents();
        initUserComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblAssignment = new javax.swing.JTable();

        setDoubleBuffered(false);
        setLayout(new java.awt.BorderLayout());

        tblAssignment.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Assignment", "Start Time", "Deadline"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblAssignment.setColumnSelectionAllowed(true);
        tblAssignment.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tblAssignment.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblAssignment.setShowVerticalLines(false);
        tblAssignment.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAssignmentMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblAssignment);
        tblAssignment.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void tblAssignmentMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAssignmentMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            int selectedRow = tblAssignment.rowAtPoint(evt.getPoint());
            System.err.println(selectedRow);
            ProblemFrame problemFrame = new ProblemFrame(bean, asmtIDList.get(selectedRow));
        }

    }//GEN-LAST:event_tblAssignmentMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblAssignment;
    // End of variables declaration//GEN-END:variables

    private void initUserComponents() {
    }

    public void refresh() {
        DefaultTableModel model = (DefaultTableModel) tblAssignment.getModel();

        int numRows = model.getRowCount();
        while (model.getRowCount() > 0) {
            model.removeRow(0);
            tblAssignment.revalidate();
        }

        String[] items = {"Assignment1", new Date().toString(), "48 / 60"};
        String[] items2 = {"Assignment2", new Date().toString(), "23 / 40"};
        String[] items3 = {"Assignment3", new Date().toString(), "55 / 80"};

        model.addRow(items);
        model.addRow(items2);
        model.addRow(items3);

    }

    void refresh(PASRemote bean) {
        DefaultTableModel model = (DefaultTableModel) tblAssignment.getModel();
        asmtIDList = new ArrayList<Long>();
        this.bean = bean;
        int numRows = model.getRowCount();
        while (model.getRowCount() > 0) {
            model.removeRow(0);
            tblAssignment.revalidate();
        }

        for (Assignment i : bean.getAllAssignments()) {
            try
            {
                String[] items = {i.getName(), i.getStartDate().toString(), i.getEndDate().toString()};
                model.addRow(items);
                asmtIDList.add(i.getId());
            
            }
            catch(Exception e)
            {
                // ignore item.
            }
        }

    }
}
